<?php

echo 'hello';

use Swoft\Http\Server\Exception\NotFoundRouteException;
use Swoft\Validator\Exception\ValidatorException;
use Xiaoproject\Common\Json\Json;
use Xiaoproject\Common\Json\Mapper;
use Xiaoproject\Common\Language\L;

if (!function_exists('jsonMapper')) {
    /**
     * json mapper
     * @param string $json
     * @param $classPath
     * @return object
     * @author 张伯发 2019/11/21 13:32
     */
    function jsonMapper(string $json, $classPath)
    {
        $object = new $classPath;
        $result = (new Mapper())->map($json, $object);
        return $result;
    }
}

if (!function_exists('getRandom')) {
    /**
     * 获取随机数
     * @param int $length
     * @return int
     * @author 张伯发 2019/11/21 13:56
     */
    function getRandom($length = 6): int
    {
        $min = pow(10, ($length - 1));
        $max = pow(10, $length) - 1;
        return mt_rand($min, $max);
    }
}

if (!function_exists('obj2Array')) {
    /**
     * @param $obj
     * @param bool $removeNull
     * @param bool $toUnderScore
     * @return array
     * @throws ReflectionException
     * @author 张伯发 2019/11/22 18:37
     */
    function obj2Array($obj, bool $removeNull=false, bool $toUnderScore = true)
    {
        $reflect = new \ReflectionClass($obj);
        $props = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED);
        $array = [];
        foreach ($props as $prop) {
            $prop->setAccessible(true);
            $key = $prop->getName();
            $value = $prop->getValue($obj);

            if ($removeNull == true && $value === null) continue;

            if (is_object($value)) $value = obj2Array($value); // 递归转换

            if ($toUnderScore === true) $key = toUnderScore($key);

            $array[$key] = $value;
        }
        return $array;
    }
}


if (!function_exists('toUnderScore')) {
    function toUnderScore($str)
    {
        $temp = preg_replace_callback('/([A-Z]+)/',function($matchArr)
        {
            return '_'.strtolower($matchArr[0]);
        },$str);
        return trim(preg_replace('/_{2,}/','_',$temp),'_');
    }
}



if (!function_exists('handleAppError')) {

    /**
     * @param Throwable $e
     * @return array
     * @author 张伯发 2019/11/21 14:27
     */
    function handleAppError(Throwable $e) : ?array
    {
        $errorMsg = $e->getMessage();
        // JSON 格式化异常
        if ($e instanceof \InvalidArgumentException) {
            $has = strpos($errorMsg,'JsonMapper::map() requires first argument to be an object');

            if ($has !== false) {
                return Json::E(L::get('json_mapper_error'). '= '. $errorMsg);
            }
        }
        // JSON 格式化异常
        if ($e instanceof \JsonMapper_Exception) {
            return Json::E(L::get('json_mapper_error'). '= '. $errorMsg);
        }

        // 接口不存在
        if ($e instanceof NotFoundRouteException) {
            return Json::E(L::get('route_not_found'));
        }

        // 验证异常
        if ($e instanceof ValidatorException) {
            return Json::E($e->getMessage());
        }
        return null;
    }
}
